import request from "@/utils/request";
/**
 * 获取组件库地址
 */
function getFisUrl() {
  return request({
    url: "/admin/system/config/base/detail/businessComponentFilsUrl",
    method: "get",
  });
}
export async function getRemoteComps(components = {}) {
  const { configValue } = (await getFisUrl())?.data || {};
  const resp = await request({
    url: configValue,
    method: "post",
    data: {
      components: Object.values(components),
    },
  });
  if (resp?.code !== 200) {
    return {
      components: {},
    };
  }
  const comps = resp.data;
  const baseCompObj = {
    name: "RemoteComponentsLoader",
    config: {
      name: "",
      description: "",
      js: "",
      css: "",
    },
  };
  const keys = Object.keys(comps);
  const remotes = keys.map((key) => {
    const { js, css } = comps[key];
    return {
      ...baseCompObj,
      name: key,
      config: {
        name: key,
        css,
        js,
      },
    };
  });
  const obj = {};
  remotes.forEach((item) => {
    obj[item.config.name] = item;
  });
  return {
    components: obj,
    loaded: true,
  };
}
